package chenji.yourticket.configuration.configurations;

import java.io.IOException;
import java.util.HashSet;

import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;

import chenji.yourticket.Main;
import chenji.yourticket.configuration.ConfigException;
import chenji.yourticket.configuration.KConfiguration;

public class Config extends KConfiguration {
	private String anti_counterfeitingl;
	private int maxLoreLine;
	private int lengthLimit;

	public Config() throws ConfigException {
		super("config.yml", true, "成功创建Config.yml", "无法创建Config.yml");
		loadConfig();
		try {
			this.saveConfig();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void loadConfig() {
		getMaterialInfo();
		this.loadLimit();
		this.loadVIPDiscount();

		this.maxLoreLine = this.getConfig().getInt("Global.maxLoreLine");
		this.anti_counterfeitingl = this.getConfig().getString("Global.anti-counterfeiting");
		this.lengthLimit = this.getConfig().getInt("Global.lengthLimit");
	}

	public String getanti_counterfeiting() {
		return this.anti_counterfeitingl;
	}

	public int getmaxLoreLine() {
		return this.maxLoreLine;
	}

	public int getLengthLimit() {
		return this.lengthLimit;
	}

	public void getMaterialInfo() {
		if (!(this.getConfig().contains("Global.ArrowTicketMaterial"))) {
			Main.getLog().warning("未加载到任何允许的门票材质。即将按照默认的材质信息进行设置：");
			Main.getLog().warning("设置纸为唯一允许的材质，每张印刷费用为0.4游戏币，如果需要修改");
			Main.getLog().warning("请前往config.yml进行你必要的修改，谢谢合作。");
			this.getConfig().set("Global.ArrowTicketMaterial.PAPER.price", 0.4);
		}
		int loadInfo = 0;
		for (String Material : this.getConfig().getConfigurationSection("Global.ArrowTicketMaterial").getKeys(false)) {
			Material m = null;
			StringBuilder b = new StringBuilder();
			b.append("Global.ArrowTicketMaterial").append(".").append(Material);
			String root = b.toString();
			try {
				m = Enum.valueOf(Material.class, Material);
			} catch (Exception ex) {
				Main.getLog().warning("对不起，加载材质" + Material + "时遇到异常，其不是一个标准的Material名称。已经将其清除。");
				this.getConfig().set(root, null);
				continue;
			}
			double price = this.getConfig().getDouble(new StringBuilder().append(root).append(".price").toString());
			if (price == 0) {
				Main.getLog().warning("警告：加载材质" + Material + "时遇到异常，price为0，请注意。");

			}
			HashSet<Enchantment> Enchlist = new HashSet<Enchantment>();
			Main.getMaterialManager().registerMaterialInfo(Enchlist, m, price);
			loadInfo++;

		}
		Main.getLog().info(new StringBuilder().append("共加载").append(loadInfo).append("个材质信息。").toString());
	}

	public void loadVIPDiscount() {
		if (!this.getConfig().contains("Global.VIPDiscount")) {
			Main.getLog().info("未加载任何VIP折扣信息。");
			return;
		}
		int loadInfo = 0;
		for (String key : this.getConfig().getConfigurationSection("Global.VIPDiscount").getKeys(false)) {
			String path = "Global.VIPDiscount." + key;
			double Discount = this.getConfig().getDouble(path);
			if (Discount == 0) {
				Main.getLog().warning(
						new StringBuilder().append("警告，加载折扣：").append(key).append("时遇到异常，折扣为0,请注意。").toString());
			}
			if (Discount < 0) {
				Main.getLog().warning(
						new StringBuilder().append("警告，加载折扣：").append(key).append("时遇到异常，折扣不能小于0,已经将其清除。").toString());
				this.getConfig().set(path, null);
			}
			Main.getVIPDiscountManager().addVipDiscount(key, Discount);
			loadInfo++;
		}
		Main.getLog().info(new StringBuilder().append("共加载").append(loadInfo).append("个折扣信息。").toString());
	}

	public void loadLimit() {
		int defaultLimit = this.getConfig().getInt("Global.DefaultLimit");
		if (defaultLimit == 0) {
			Main.getLog().warning("警告，默认限制数量为0，这会默认状况下玩家无法使用本插件。");
		}else{
			Main.getLimitManager().setDefaultLimit(defaultLimit);
		}

		if (!this.getConfig().contains("Global.VIPLimitList")) {
			Main.getLog().info("未加载任何VIP提高限制信息。");
			return;
		}
		int loadInfo = 0;
		for (String key : this.getConfig().getConfigurationSection("Global.VIPLimitList").getKeys(false)) {
			String path = "Global.VIPLimitList." + key;
			int Limit = this.getConfig().getInt(path);
			if (Limit == 0) {
				Main.getLog().warning(
						new StringBuilder().append("警告，加载上限：").append(key).append("时遇到异常，上限为0,请注意。").toString());
			}
			if (Limit < 0) {
				if (!(Limit == -1)) {
					Main.getLog().warning(new StringBuilder().append("警告，加载上限：").append(key)
							.append("时遇到异常，上限不能小于0,已经将其清除。").toString());
					this.getConfig().set(path, null);
				}
			}
			Main.getLimitManager().addLimit(key, Limit);
			loadInfo++;
		}
		Main.getLog().info(new StringBuilder().append("共加载").append(loadInfo).append("个上限信息。").toString());
	}
}
